/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.ChiTietMuaNgay;
import pojo.Sanpham;
import pojo.Taikhoan;
import util.HibernateUtil;

/**
 *
 * @author WMThiet
 */
public class ChiTietMuaNgayDAO {
    public static boolean ThemMuaNgay(Sanpham sanPham, Taikhoan taiKhoan){
        
        if(KiemTraTonTai(sanPham.getMaSanPham(),taiKhoan.getMaTaiKhoan()) == true){
            return false;
        }            
        
        boolean result = false;
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            
            ChiTietMuaNgay chiTietMuaNgay = new ChiTietMuaNgay();
            chiTietMuaNgay.setSanPham(sanPham);
            chiTietMuaNgay.setTaiKhoan(taiKhoan);
            chiTietMuaNgay.setDaKiemDuyet(false);
            chiTietMuaNgay.setNgayMua(new Date());            
            session.save(chiTietMuaNgay);     
            
            //Đấu giá kết thúc:
            Sanpham sp = (Sanpham)session.get(Sanpham.class, sanPham.getMaSanPham());
            sp.setNgayHetHan(new Date());
            sp.setDaMuaNgay(true);
            session.update(sp);
            
            //Cập nhật điểm cho người đăng sản phẩm và người đặt mua:
            Taikhoan taiKhoanNguoiDang = (Taikhoan) session.get(Taikhoan.class, sanPham.getTaikhoan().getMaTaiKhoan());
            taiKhoanNguoiDang.setDiemTinCayBan(taiKhoanNguoiDang.getDiemTinCayBan() + 3);
            session.update(taiKhoanNguoiDang);
            
            Taikhoan taiKhoanNguoiMua = (Taikhoan) session.get(Taikhoan.class, taiKhoan.getMaTaiKhoan());
            taiKhoanNguoiMua.setDiemTinCayMua(taiKhoanNguoiDang.getDiemTinCayMua() + 3);
            session.update(taiKhoanNguoiMua);
            
            transaction.commit();
            
            result = true;
            
        } catch (HibernateException ex) {
            transaction.rollback(); 
            result = false;
        } finally {
            session.close();
        }
        return result;
    }
    
    public static boolean KiemTraTonTai(int masp,int matk){
        boolean result = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        try {
            String hql = "from ChiTietMuaNgay ct where ct.sanPham.maSanPham =:masp";
            Query query = session.createQuery(hql);
            query.setInteger("masp", masp);
                                    
            List<ChiTietMuaNgay> listCTMN = query.list();
            if(listCTMN != null && listCTMN.isEmpty() == false){
                ChiTietMuaNgay ctmn = listCTMN.get(0);
                
                if(ctmn.getDaKiemDuyet() == true || ctmn.getTaiKhoan().getMaTaiKhoan() == matk){
                    result = true;
                }
            }
            
        } catch (HibernateException ex) {
            result = false;
        } finally {
            session.close();
        }
        return result;
    }
    
    public static ChiTietMuaNgay LayChiTietMuaNgayGanNhat() {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        ChiTietMuaNgay ctMuaNgay = null;
        try{            
            session.beginTransaction();            
            String hql = "from ChiTietMuaNgay";
            Query query = session.createQuery(hql);
            List<ChiTietMuaNgay> listMuaNgay = query.list();
            ctMuaNgay = listMuaNgay.get(listMuaNgay.size() - 1);
        }
        catch(Exception ex){
            ctMuaNgay = null;
        }finally{
            session.close();
        }
        
        return ctMuaNgay;
    }
}
